package com.barbeque.mall.service.wx;

import com.barbeque.mall.bean.goods.MarketGoods;
import com.barbeque.mall.bean.goods.MarketGoodsExample;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.wx.goods.MarketCategoryBean;
import com.barbeque.mall.bean.wx.goods.WxGoodsListBO;
import com.barbeque.mall.bean.wx.goods.WxGoodsListVO;
import com.barbeque.mall.mapper.MarketGoodsMapper;
import com.barbeque.mall.mapper.WxMarketCategoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/20 23:43
 */
@Service
public class WxGoodsServiceImpl implements WxGoodsService {

    private static final Integer DEFAULT_PAGE_NUM = 1;
    private static final Integer DEFAULT_PAGE_SIZE = 6;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    WxMarketCategoryMapper wxMarketCategoryMapper;

    /**
     * 获取goods中有new标记的货物
     *
     * @return java.util.List<com.barbeque.mall.bean.goods.MarketGoods>
     * @author 苏东宇
     * @since 2022/07/21 9:19
     */
    @Override
    public List<MarketGoods> getNewGoodsList() {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIsNewEqualTo(true);
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(1, 6);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);


        return marketGoods;
    }

    /**
     * 获取热门商品
     *
     * @return java.util.List<com.barbeque.mall.bean.goods.MarketGoods>
     * @author 苏东宇
     * @since 2022/07/21 9:44
     */
    @Override
    public List<MarketGoods> getHotGoodsList() {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIsHotEqualTo(true);
        criteria.andDeletedEqualTo(false);
        PageHelper.startPage(1, 6);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);


        return marketGoods;
    }

    /**
     * 根据categoryid获取goods
     *
     * @param categoryId
     * @return java.util.List<com.barbeque.mall.bean.goods.MarketGoods>
     * @author 苏东宇
     * @since 2022/07/21 10:36
     */
    @Override
    public List<MarketGoods> getGoodsByCategoryId(Integer categoryId) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        criteria.andDeletedEqualTo(false);

        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);


        return marketGoods;
    }

    /**
     * 获取所有商品
     *
     * @return java.util.List<com.barbeque.mall.bean.goods.MarketGoods>
     * @author 苏东宇
     * @since 2022/07/21 11:46
     */
    @Override
    public List<MarketGoods> getAllGoods() {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);

        return marketGoods;


    }

    /**
     * 根据id查询商品信息
     *
     * @param goodsId
     * @return java.util.List<com.barbeque.mall.bean.goods.MarketGoods>
     * @author 苏东宇
     * @since 2022/07/21 17:06
     */
    @Override
    public MarketGoods getGoodsByGoodsId(Integer goodsId) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIdEqualTo(goodsId);
        criteria.andDeletedEqualTo(false);

        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(goodsId);

        return marketGoods;
    }

    /**
     * wx/goods/list 分页、商品简要信息、类目信息
     *
     * @param wxGoodsListBO
     * @return com.barbeque.mall.bean.wx.goods.WxGoodsListVO
     * @author Euphonium
     * @since 2022/07/21
     */
    @Override
    public WxGoodsListVO getGoodsLit(WxGoodsListBO wxGoodsListBO) {
        PageHelper.startPage(wxGoodsListBO.getPage(), wxGoodsListBO.getLimit());
        MarketGoodsExample goodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria goodsExampleCriteria = goodsExample.createCriteria();
        // 根据brandId查询
        if (wxGoodsListBO.getBrandId() != null) {
            goodsExampleCriteria.andBrandIdEqualTo(wxGoodsListBO.getBrandId());
        }
        // 根据categoryId查询
        if (wxGoodsListBO.getCategoryId() != null && wxGoodsListBO.getCategoryId() != 0) {
            goodsExampleCriteria.andCategoryIdEqualTo(wxGoodsListBO.getCategoryId());
        }
        // 根据isHot查询
        if (wxGoodsListBO.getIsHot() != null) {
            goodsExampleCriteria.andIsHotEqualTo(wxGoodsListBO.getIsHot());
            goodsExample.setOrderByClause(wxGoodsListBO.getSort() + " " + wxGoodsListBO.getOrder());
        }
        // 根据isNew查询
        if (wxGoodsListBO.getIsNew() != null) {
            goodsExampleCriteria.andIsHotEqualTo(wxGoodsListBO.getIsNew());
            goodsExample.setOrderByClause(wxGoodsListBO.getSort() + " " + wxGoodsListBO.getOrder());
        }
        // 根据keyword查询
        if (wxGoodsListBO.getKeyword() != null) {
            goodsExampleCriteria.andNameLike("%" + wxGoodsListBO.getKeyword() + "%");
            goodsExample.setOrderByClause(wxGoodsListBO.getSort() + " " + wxGoodsListBO.getOrder());
        }
        List<MarketGoods> list = marketGoodsMapper.selectByExample(goodsExample);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(list);
        WxGoodsListVO wxGoodsListVO = new WxGoodsListVO();
        wxGoodsListVO.setTotal((int) pageInfo.getTotal());
        wxGoodsListVO.setPages(pageInfo.getPages());
        wxGoodsListVO.setPage(pageInfo.getPageNum());
        wxGoodsListVO.setLimit(pageInfo.getPageSize());
        wxGoodsListVO.setList(list);
        // 获取筛选出的商品的所属类目
        List<Integer> categoryIds = new LinkedList<>();
        for (MarketGoods marketGoods : list) {
            Integer categoryId = marketGoods.getCategoryId();
            if (!categoryIds.contains(categoryId)) {
                categoryIds.add(categoryId);
            }
        }
        // 根据类目ID，获取所有的类目信息
        List<MarketCategoryBean> filterCategoryList = new LinkedList<>();
        for (Integer id : categoryIds) {
            MarketCategoryBean filterCategory = wxMarketCategoryMapper.selectByPrimaryKey(id);
            filterCategoryList.add(filterCategory);
        }
        wxGoodsListVO.setFilterCategoryList(filterCategoryList);
        return wxGoodsListVO;
    }

    /**
     * 获取相关商品信息(同一类目)
     *
     * @param id
     * @return com.barbeque.mall.bean.param.CommonData<com.barbeque.mall.bean.wx.goods.WxGoodsBriefInfoData>
     * @author Euphonium
     * @since 2022/07/21
     */
    @Override
    public CommonData<MarketGoods> getRelatedGoods(Integer id) {
        Integer categoryId = marketGoodsMapper.selectCategoryIdByPrimaryKey(id);
        MarketGoodsExample example = new MarketGoodsExample();
        example.createCriteria().andCategoryIdEqualTo(categoryId);
        PageHelper.startPage(DEFAULT_PAGE_NUM, DEFAULT_PAGE_SIZE);
        List<MarketGoods> marketGoodsList = marketGoodsMapper.selectByExample(example);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(marketGoodsList);
        return CommonData.data(pageInfo);
    }
}
